<script setup lang="ts">
// import type { VideoItem } from '@/types/video'
// Nuxt使用useFetch请求数据，不是使用axios，默认GET请求，可以不用写这段　{method: 'GET'}省略
// 通过 useFetch 获取频道列表数据，data 是响应式数据，可以直接用于界面渲染
// useFetch('/api/channel',{method: 'GET'})
const { data: channelList } = await useFetch('/api/channel')
// console.log('频道列表',channelList)
// 视频列表
const { data: videoList } = await useFetch('/api/video')
console.log('视频',videoList)

const list = ref([])
const loading = ref(false) // 加载状态
const finished = ref(false) // 是否完成
let page = 1 // 页码
let pageSize = 20 // 条数
// 滚动页面触底触发
const onLoad = () => {
  loading.value = false
  // console.log('滚动触底')
  // 根据页码提前数据
  const data = videoList.value?.slice((page - 1) * pageSize, page * pageSize)
  // 数据追加
  list.value.push(...data)
  // 页码累加
  page++
  // 加载完成，停止滚动加载效果
  if(videoList.value?.length === list.value.length){
    finished.value = true
  }
}
// 首页一打开（页面初始化） - 主动请求前 20条数据，用于首屏渲染，，方便SEO抓取数据
onLoad()
</script>

<template>
  <!--公共头部-->
  <header class="app-header">
    <NuxtLink class="logo" to="/">
      <img src="@/assets/images/logo.png" />
    </NuxtLink>
    <a class="search"><i class="iconfont icon-search"></i></a>
    <a class="face">登录</a>
    <a class="down-app">下载APP</a>
  </header>
  <!--频道-->
  <van-tabs>
    <van-tab v-for="item in channelList" :key="item.id" :title="item.name"></van-tab>
  </van-tabs>
  <!--视频列表-->
  <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
    <div class="video-list">
      <NuxtLink class="v-card" v-for="item in list" :key="item.aid" :to="`/video/${item.aid}`">
        <div class="card">
          <div class="card-img">
            <img class="pic" :src="item.pic" :alt="item.title" />
          </div>
          <div class="count">
            <span><i class="iconfont icon-shipin_bofangshu icon-lock"></i>{{item.stat.view}}万</span>
            <span><i class="iconfont icon-shipin_danmushu icon-dianzan"></i>{{item.stat.danmaku}}万</span>
          </div>
        </div>
        <p class="title text-ellipsis-2">{{item.title}}</p>
      </NuxtLink>
      <!--<AppVideo v-for="item in list" :key="item.bvid" :item="item"></AppVideo>-->
    </div>
  </van-list>
</template>

<style lang="scss" scoped>
/*公共头部*/
.app-header{
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background-color: #fff;
  .logo{
    flex: 1;
    .Navbar_logo{
      color: #fb7299;
      font-size: 28px;
    }
    img{
      width: 60px;
      height: 30px;
    }
  }
}
.search{
  margin-top: 3px;
  margin-right: 10px;
}
.face{
  font-size: 12px;
  color: #333;
  padding: 5px 2px;
  background-color: #ddd;
  border-radius: 50%;
  margin-right: 10px;
}
.down-app{
  font-size: 12px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fb7299;
  color: #fff;
  border-radius: 5px;
  padding: 5px 10px;
}
.video-list{
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  margin-top: 10px;
}
/*视频卡片*/
.v-card{
  width: 50%;
  padding: 0 5px 10px;
  .card{
    position: relative;
    background: #f3f3f3 url(@/assets/images/default.png) center no-repeat;
    background-size: 36%;
    border-radius: 2px;
    overflow: hidden;
    .card-img{
      .pic{
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    .count{
      width: 100%;
      position: absolute;
      left: 0;
      bottom: 0;
      z-index: 2;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 5px;
      font-size: 12px;
      color: #fff;
      background-image: linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,1));
    }
  }
  .title{
    font-size: 14px;
    color: #333;
    margin-top: 5px;
  }
}
</style>
